package com.example.myapplication;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import ohos.ace.adapter.ALog;
import ohos.ace.adapter.capability.bridge.BridgePlugin;
import ohos.ace.adapter.capability.bridge.IMessageListener;
import ohos.ace.adapter.capability.bridge.IMethodResult;
import ohos.ace.adapter.capability.bridge.MethodData;

public class Bridge extends BridgePlugin implements IMessageListener, IMethodResult {
    private AudioPlayer audioPlayer = new AudioPlayer();

    /**
     * @param context
     * @param bridgeName
     * @param instanceId
     * @deprecated
     */
    public Bridge(Context context, String bridgeName, int instanceId) {
        super(context, bridgeName, instanceId);
        setMessageListener(this);
        setMethodResultListener(this);
    }

    public String methodOfPlatform(){
        //ALog.i("Android call Bridge methodOfPlatform: ","methodOfTs");
        Object[] paramObject = {};
        MethodData methodData = new MethodData("methodOfPlatform", paramObject);
        callMethod(methodData);
        return "android call platform method.";
    }

    public String methodOfPlatform1(){
        Object[] paramObject = {};
        MethodData methodData = new MethodData("getString", paramObject);
        callMethod(methodData);
        return "android call platform method getString.";
    }

    public String playMusic(Object object) {
        String s = object.toString();
        JSONObject json = JSON.parseObject(s);
        String path = json.getString("path");
        Log.e("HiHelloWorld", "playMusic " + path);
        audioPlayer.play(path);
        return "播放音乐：" + json.getString("title");
    }

    public String stop(Object object) {
        audioPlayer.stop();
        return "暂停音乐";
    }

    // 注册回调，监听ArkUI侧的数据传递
    @Override
    public Object onMessage(Object data) {
        ALog.i("onMessage data:", data.toString());
        if("android call platform method.".equals(data.toString())){
            this.methodOfPlatform1();
            return "android call platform method.";
        }
        return "android onMessage success";
    }

    @Override
    public void onMessageResponse(Object data) {
        ALog.i("onMessageResponse data:", data.toString());
    }

    @Override
    public void onSuccess(Object object) {
        if (object == null) {
            ALog.i("bridge return data is null", "call Success");
            sendMessage("PlatformBridge successfully calls method of TS, The method is of type void");
        } else {
            ALog.i("bridge onSuccess data:", object.toString());
            String result =
                "PlatformBridge successfully calls method of TS, and the return value is " + object.toString();
            sendMessage(result);
        }
    }

    @Override
    public void onError(String methodName, int errorCode, String errorMessage) {
        ALog.e("Bridge:", "onError: " + methodName + " " + String.valueOf(errorCode) + " " + errorMessage);
        sendMessage("onError: " + methodName + " " + String.valueOf(errorCode) + " " + errorMessage);
    }

    @Override
    public void onMethodCancel(String methodName) {
        ALog.i("Bridge:", "onMethodCancel");
        sendMessage("onMethodCancel");
    }
}